package Lock;

public class DeadLock implements Runnable{

    static  Object a = new Object();
    static  Object b = new Object();
    Boolean flag;

    public DeadLock(boolean flag){
        this.flag = flag;
    }
    @Override
    public void run() {
        if (flag) {
            while (true) {
                synchronized (a) {
                    System.out.println("执行a锁");
                    synchronized (b) {
                        System.out.println("执行b锁");
                    }
                }
            }
        } else {
            while (true) {
                synchronized (b) {
                    System.out.println("执行b锁");
                    synchronized (a) {
                        System.out.println("执行a锁");
                    }
                }
            }
        }
    }
    public static void main(String[] args) {

        DeadLock thread1 = new DeadLock(true);
        DeadLock thread2 = new DeadLock(false);

        new Thread(thread1,"a").start();
        new Thread(thread2,"b").start();

    }

}
